package com.jcodecraeer.xrecyclerview.progressindicator.indicator;

import ohos.agp.animation.Animator;
import ohos.agp.animation.AnimatorValue;
import ohos.agp.render.Arc;
import ohos.agp.render.Canvas;
import ohos.agp.render.Paint;
import ohos.agp.utils.RectFloat;

import java.util.ArrayList;
import java.util.List;

public class PacmanIndicator extends BaseIndicatorController {

    private float translateX;

    private int alpha;

    private float degrees1,degrees2;
    @Override
    public void draw(Canvas canvas, Paint paint) {
        drawPacman(canvas,paint);
        drawCircle(canvas,paint);
    }

    private void drawPacman(Canvas canvas,Paint paint){
        float x=getWidth()/2;
        float y=getHeight()/2;

        canvas.save();

        canvas.translate(x, y);
        canvas.rotate(degrees1,0,0);
        paint.setAlpha(255);
        RectFloat rectF1=new RectFloat(-x/1.7f,-y/1.7f,x/1.7f,y/1.7f);
        Arc mArc = new Arc();
        mArc.setArc(0, 270, true);
        canvas.drawArc(rectF1, mArc, paint);

        canvas.restore();

        canvas.save();
        canvas.translate(x, y);
        canvas.rotate(degrees2,0,0);
        paint.setAlpha(255);
        RectFloat rectF2=new RectFloat(-x/1.7f,-y/1.7f,x/1.7f,y/1.7f);
        Arc arc = new Arc();
        arc.setArc(90,270,true);
        canvas.drawArc(rectF2,arc,paint);
        canvas.restore();
    }


    private void drawCircle(Canvas canvas, Paint paint) {
        float radius=getWidth()/11;
        paint.setAlpha(alpha);
        canvas.drawCircle(translateX, getHeight() / 2, radius, paint);
    }


    @Override
    public List<Animator> createAnimation() {
        List<Animator> animators=new ArrayList<>();
        float startT=getWidth()/11;
        AnimatorValue translationAnim = new AnimatorValue();
        translationAnim.setDuration(650);
        translationAnim.setCurveType(Animator.CurveType.SMOOTH_STEP);
        translationAnim.setLoopedCount(-1);
        translationAnim.setValueUpdateListener(new AnimatorValue.ValueUpdateListener() {
            @Override
            public void onUpdate(AnimatorValue animatorValue, float v) {
                translateX = v;
                postInvalidate();
            }
        });
        translationAnim.start();

        AnimatorValue alphaAnim =new AnimatorValue();
        alphaAnim.setDuration(650);
        alphaAnim.setLoopedCount(-1);
        alphaAnim.setValueUpdateListener(new AnimatorValue.ValueUpdateListener() {
            @Override
            public void onUpdate(AnimatorValue animatorValue, float v) {
                alpha = (int) v;
                postInvalidate();
            }
        });
        alphaAnim.start();

        AnimatorValue rotateAnim1 = new AnimatorValue();
        rotateAnim1.setDuration(650);
        rotateAnim1.setLoopedCount(-1);
        rotateAnim1.setValueUpdateListener(new AnimatorValue.ValueUpdateListener() {
            @Override
            public void onUpdate(AnimatorValue animatorValue, float v) {
                degrees1 = v;
                postInvalidate();
            }
        });
        rotateAnim1.start();

        AnimatorValue rotateAnim2 = new AnimatorValue();
        rotateAnim2.setDuration(650);
        rotateAnim2.setLoopedCount(-1);
        rotateAnim2.setValueUpdateListener(new AnimatorValue.ValueUpdateListener() {
            @Override
            public void onUpdate(AnimatorValue animatorValue, float v) {
                degrees2 = v;
                postInvalidate();
            }
        });
        rotateAnim2.start();

        animators.add(translationAnim);
        animators.add(alphaAnim);
        animators.add(rotateAnim1);
        animators.add(rotateAnim2);
        return animators;
    }
}
